﻿//===============================================================================
// File: ControllerHelper.cs
// Author:Veiller Hu
// CLR Version：4.0.30319.1
// Created Date:  2011/2/20 3:19:50 PM 
// Summary:  
//===============================================================================
//
// Copyright (c) East China Information Technology Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GPMS.BLL
{
    using GPMS.Extension;
using GPMS.Model;
    public static class ControllerHelper
    {
        /// <summary>
        /// 获得ControllerInfo实体
        /// </summary>
        /// <param name="id">序号</param>
        /// <param name="db">EF</param>
        /// <returns></returns>
        public static GPMS.Model.ControllerInfo Get(Guid id,GPMS.Model.ExtendedGPMSDb db)
        {
            return db.ControllerInfos.FirstOrDefault(c => c.ControllerID == id);
        }

        public static List<ControllerInfo> GetList(GPMS.Model.ExtendedGPMSDb db)
        {
            return db.ControllerInfos.Where(c => c.IsDeleted == false).ToList();
        }

        public static bool IsExist(Guid id, GPMS.Model.ExtendedGPMSDb db)
        {
            return db.ControllerInfos.Any(c => c.ControllerID == id && c.IsDeleted == false);
        }
        public static bool IsExist(string controllerName, GPMS.Model.ExtendedGPMSDb db)
        {
            return db.ControllerInfos.Any(c => c.ControllerName == controllerName && c.IsDeleted == false);
        }

        public static Guid Add(GPMS.Model.ControllerInfo controller, GPMS.Model.ExtendedGPMSDb db)
        {
            if (controller.ControllerID==Guid.Empty)
            {
                controller.ControllerID = Guid.NewGuid();
            }
            if (IsExist(controller.ControllerName,db))
            {
                throw new DuplicateException();
            }
            controller.CreateDate = DateTime.Now;
            controller.IsDeleted = false;
            db.ControllerInfos.AddObject(controller);
            db.SaveChanges();
            return controller.ControllerID;
        }

        public static void Deleted(Guid id, GPMS.Model.ExtendedGPMSDb db)
        {
            GPMS.Model.ControllerInfo controller = db.ControllerInfos.FirstOrDefault(c => c.ControllerID == id);
            controller.IsDeleted = true;
            db.SaveChanges();
        }

    }
}
